from PySide6.QtWidgets import QSlider, QLabel, QWidget, QHBoxLayout, QVBoxLayout
from PySide6.QtCore import Signal, QSize, Qt
from components.base_component import DraggableComponent

class SliderComponent(DraggableComponent):
    """滑动条组件"""
    slider_value_changed = Signal(str, int)  # 滑动条ID，值
    
    def __init__(self, component_id, label, min_val, max_val, default_val, unit="", parent=None):
        super().__init__(component_id, parent)
        self.title_label.setText(f"滑动条: {component_id}")
        self.unit = unit  # 添加单位属性
        
        # 设置滑动条组件的最小尺寸
        self.setMinimumSize(400, 170)  # 增加最小宽度
        
        # 确保内容区域足够大
        self.content.setMinimumHeight(100)
        
        # 主容器 - 垂直布局
        main_container = QWidget()
        main_container.setStyleSheet("border: none; background-color: transparent;")
        main_layout = QVBoxLayout(main_container)
        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.setSpacing(15)
        
        # 添加标签
        self.label = QLabel(label)
        self.label.setStyleSheet("font-size: 15px; font-weight: bold; color: #333; border: none; padding: 5px;")
        self.label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.label.setMinimumHeight(25)
        self.label.setMinimumWidth(200)  # 增加标签最小宽度
        self.label.setWordWrap(False)    # 防止标签换行
        main_layout.addWidget(self.label)
        
        # 创建水平布局用于滑动条和值显示
        slider_container = QWidget()
        slider_container.setStyleSheet("border: none; background-color: transparent;")
        slider_container.setMinimumHeight(50)
        slider_layout = QHBoxLayout(slider_container)
        slider_layout.setContentsMargins(5, 0, 5, 0)
        slider_layout.setSpacing(15)
        
        # 添加滑动条 - 增大滑动条尺寸
        self.slider = QSlider(Qt.Orientation.Horizontal)
        self.slider.setMinimum(min_val)
        self.slider.setMaximum(max_val)
        self.slider.setValue(default_val)
        self.slider.setMinimumHeight(40)
        self.slider.setMinimumWidth(230)
        self.slider.setStyleSheet("""
            QSlider::groove:horizontal {
                height: 14px;
                background: #d0d0d0;
                border-radius: 7px;
                margin: 0px;
            }
            QSlider::handle:horizontal {
                background: #0078d7;
                border: 1px solid #0078d7;
                width: 24px;
                height: 24px;
                margin: -5px 0;
                border-radius: 12px;
            }
            QSlider::handle:horizontal:hover {
                background: #1089e8;
            }
        """)
        self.slider.valueChanged.connect(self._on_value_changed)
        slider_layout.addWidget(self.slider, 4)
        
        # 添加值显示 - 增大显示区域和字体
        value_text = f"{default_val}{' ' + unit if unit else ''}"
        self.value_label = QLabel(value_text)
        self.value_label.setMinimumWidth(60)
        self.value_label.setMinimumHeight(35)
        self.value_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.value_label.setStyleSheet("""
            font-size: 18px;
            font-weight: bold;
            color: #0078d7;
            background-color: #f0f0f0;
            border: 1px solid #d0d0d0;
            border-radius: 4px;
            padding: 5px;
            margin: 2px;
        """)
        slider_layout.addWidget(self.value_label, 1)
        
        # 添加滑动条布局到主布局
        main_layout.addWidget(slider_container)
        
        # 添加主容器到内容布局
        self.content_layout.addWidget(main_container)
    
    def _on_value_changed(self, value):
        """滑动条值改变处理"""
        # 添加单位显示
        value_text = f"{value}{' ' + self.unit if self.unit else ''}"
        self.value_label.setText(value_text)
        self.slider_value_changed.emit(self.component_id, value)
    
    def get_value(self):
        """获取滑动条值"""
        return self.slider.value()
    
    def set_value(self, value):
        """设置滑动条值"""
        self.slider.setValue(value)
    
    def sizeHint(self):
        """提供滑动条组件的默认尺寸建议"""
        return QSize(420, 170) 